<div class="componentheading"><?php echo $page_title;?></div>
<div><?php global_message(); ?></div>
<?php
	$isEdit = isset($_cat) ? true : false;
	$text = empty($isEdit) ? 'Add' : 'Update';
	$selected = 'selected = "selected"';
?>
<form method="post" id="newForm">
<div class="box-header"></div>
<div class="box table">
<table width="100%">
	<tr>
    	<td width="21%"><strong>Parent Thematic Area</strong></td>
        <td width="79%">
            <?php if($isEdit){
                $_catParentName ="None";
                foreach($categories as $category) :
                    if($category->id==$_cat->parent_id){$_catParentName = $category->category_name;break;}
                endforeach;
                ?>
                <input type="hidden"  name="parent_id" value="<?php echo $_cat->parent_id;?>" />
                <input type="text"  class="readonly width400" name="parent_name" id="parent_name" value="<?php echo $_catParentName;?>" />
            <?php }else{?>
            <select name="parent_id" id="parent_id" onchange="generate_code(this.value)">
            	<option value="0">None</option>
                <?php foreach($categories as $category) : ?>
                	<option value="<?php echo $category->id;?>"><?php echo $category->category_name;?></option>
                <?php endforeach; ?>
            </select>
            <?php }?>
        </td>
    </tr>
    <tr>
    	<td><strong>Code</strong></td>
        <td><input type="text" readonly class="readonly width150" name="code" id="disabled_code" value="<?php if($isEdit){ echo $_cat->code;}?>" /></td>
    </tr>
	<tr>
    	<td><strong>Area Name</strong></td>
        <td><input type="text" class="required width400" name="category_name" id="category_name" value="<?php if($isEdit) echo $_cat->category_name;?>" /></td>
    </tr>
    <!--<tr>
    	<td><strong>Code</strong></td>
        <td><input type="text" class="required" name="code" id="code" value="<?php if($isEdit) echo $_cat->code;?>"></td>
    </tr>-->
    <tr>
    	<td>&nbsp;</td>
        <td>
        	<input type="submit" name="submit_category" id="submit_category" value="<?php echo $text;?>">
            <input type="button" onclick="window.location.href = '<?php echo site_url('category');?>'" value="Back" />
        </td>
    </tr>
</table>
</div>
</form>
<script>
$(document).ready(function(){
        <?php if(!$isEdit) {?>generate_code(0);<?php }?>
	//initialize jquery validation
	$('#newForm').validate();
	});

        function generate_code(parent_id){
            if(parent_id==0) parent_id='';
            $.post("<?php echo site_url('category/ajax_generate_code');?>", {parent_id:parent_id },function(data) {
                 $("#disabled_code").val(data);
                
           });
        }
</script>
